package com.sentiment.report;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.json.JSONObject;

import com.sentiment.database.bean.CrawlInfo;
import com.sentiment.database.bean.CrawlNews;
import com.sentiment.tools.Generator;

public class DataStatistics {
	// 热点舆情监督搜索图表结果
	public static List<JSONObject> mediaGraph(List<CrawlNews> list) {
		return mediaRatio2JsonList(mediaRatioTest());
	}

	// 每日舆情调用的媒体占比
	public static Map<String, Double> mediaRatio(List<CrawlNews> list) {
		return mediaRatioParser(list);
	}

	// 热点舆情监督搜索图表结果
	public static List<JSONObject> reprintCommentGraph(List<CrawlNews> list) {
		return reprintCommentRatio2JsonList(reprintCommentRatio(list));
	}

	public static Map<String, Date> releaseTimeRatio(List<CrawlNews> list) {
		return releaseTimeRatioParser(list);
	}

	/**
	 * 评论量
	 * 
	 * @param list
	 * @return
	 */
	private static Map<String, Integer> commentRatio(List<CrawlNews> list) {
		Map<String, Integer> map = new HashMap<String, Integer>();
		for (CrawlNews n : list) {
			String publisher = n.getPublisher();
			int comment = n.getComment();
			map.put(publisher, comment);
		}
		return map;
	}

	/**
	 * 转载量与评论量
	 * 
	 * @param list
	 * @return
	 */
	private static Map<String, String> reprintCommentRatio(List<CrawlNews> list) {
		Map<String, String> map = new HashMap<String, String>();
		for (CrawlNews n : list) {
			String publisher = n.getPublisher();
			int reprint = n.getReprint();
			int comment = 0;
			if (n.getComment() != null)
				comment = n.getComment();
			else
				// 随机生成测试数据
				comment = Generator.generateInt(50, 500);
			map.put(publisher, String.valueOf(reprint) + "/" + String.valueOf(comment));
		}
		return map;
	}

	private static Map<String, Date> releaseTimeRatioParser(List<CrawlNews> list) {
		Map<String, Date> map = new HashMap<String, Date>();
		for (CrawlNews ci : list) {
			String publisher = ci.getPublisher();
			Date date = ci.getReleaseTime();
			if (date == null)
				map.put(publisher, new Date());
			map.put(publisher, date);
		}
		return map;
	}

	private static Map<String, Double> mediaRatioParser(List<CrawlNews> list) {
		// TODO Auto-generated method stub
		Map<String, Integer> map = new HashMap<String, Integer>();
		for (CrawlNews info : list) {
			String publisher = info.getPublisher();
			if (map.containsKey(publisher)) {
				int i = map.get(publisher);
				map.put(publisher, i + 1);
			} else
				map.put(publisher, 1);
		}
		Map<String, Double> result = new HashMap<String, Double>();
		int amount = 0;
		for (int value : map.values()) {
			amount += value;
		}
		for (Entry<String, Integer> entry : map.entrySet()) {
			double d = (double) entry.getValue() / amount;
			result.put(entry.getKey(), d);
		}
		return result;
	}

	private static Map<String, Double> mediaRatioTest() {
		// TODO Auto-generated method stub
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("新闻", Generator.generateInt(10, 100));
		map.put("论坛", Generator.generateInt(10, 100));
		map.put("微博", Generator.generateInt(10, 100));
		Map<String, Double> result = new HashMap<String, Double>();
		int amount = 0;
		for (int value : map.values()) {
			amount += value;
		}
		for (Entry<String, Integer> entry : map.entrySet()) {
			double d = (double) entry.getValue() / amount;
			result.put(entry.getKey(), d);
		}
		return result;

	}

	//
	// ------------------------------------转换成json的list-----------------------------------
	//

	private static List<JSONObject> commentRatio2JsonList(Map<String, Integer> map) {
		List<JSONObject> jsonObjects = new ArrayList<JSONObject>();
		for (Entry<String, Integer> entry : map.entrySet()) {
			JSONObject json = new JSONObject();
			json.put("publisher", entry.getKey());
			json.put("comment", entry.getValue());
			jsonObjects.add(json);
		}
		return jsonObjects;
	}

	/**
	 * 将map转换成json的list，内容为评论量、转载量
	 * 
	 * @param map
	 * @return
	 */
	private static List<JSONObject> reprintCommentRatio2JsonList(Map<String, String> map) {
		List<JSONObject> jsonObjects = new ArrayList<JSONObject>();
		for (Entry<String, String> entry : map.entrySet()) {
			JSONObject json = new JSONObject();
			json.put("publisher", entry.getKey());
			String[] values = entry.getValue().split("/");
			json.put("reprint", Integer.valueOf(values[0]));
			json.put("comment", Integer.valueOf(values[1]));
			jsonObjects.add(json);
		}
		return jsonObjects;
	}

	/**
	 * 媒体占比分析图
	 * 
	 * @param map
	 * @return
	 */
	private static List<JSONObject> mediaRatio2JsonList(Map<String, Double> map) {
		List<JSONObject> jsonObjects = new ArrayList<JSONObject>();
		for (Entry<String, Double> entry : map.entrySet()) {
			JSONObject json = new JSONObject();
			json.put("source", entry.getKey());
			json.put("ratio", entry.getValue());
			jsonObjects.add(json);
		}
		return jsonObjects;
	}
}
